﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Sobiens.Connectors.Entities
{
    [DataContract(Namespace = "")]
    public class ItemProperties:List<ItemProperty>
    {
        public void Add(string key, string value)
        {
            var query = from t in this
                        where t.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase) == true
                        select t;
            if (query.Count() > 0)
                throw new Exception("There is already an item with the same key");

            this.Add(new ItemProperty(key, value));
        }

        public string this[string key]
        {
            get
            {
                foreach (ItemProperty item in this)
                {
                    if (item.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase) == true)
                    {
                        return item.Value;
                    }
                }

                throw new Exception("There is no item with the given key");
            }
            set
            {
                foreach (ItemProperty item in this)
                {
                    if (item.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase) == true)
                    {
                        item.Value = value;
                        return;
                    }
                }

                throw new Exception("There is no item with the given key");
            }
        }

        public bool ContainsKey(string key)
        {
            var query = from t in this
                        where t.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase) == true
                        select t;

            if (query.Count() > 0)
                return true;
            else
                return false;
        }
    }
}
